#include <string.h>
#include "user.h"
#include "menu.h"
#include "pictures.h"
#include "fft.h"


void fftErrorMessage(int ret) {
  switch (ret) {
    case InvalidSize:
      strbox("Wrong Size (0< or >maximum or not a power of 2");
      break;
    case InvalidParams:
      strbox("Wrong parameters");
      break;
    case NoFile :
      strbox("Input file is absent");
      break;
    case WrongType :
      strbox("Input file of not allowed type");
      break;
    case InvalidPicture :
      strbox("Source picture file has the wrong structure");
      break;
    case NoDiskSpace :
      strbox("Not enough disk space to allocate output file");
      break;
    case NoMemory :
      strbox("Not enough core memory for inner buffers");
      break;
    case InvalidPicSize :
      strbox("Wrong Size (0< or >maximum or not a power of 2");
      break;
    case DifSizes :
      strbox("Two source pictures have not identical sizes for _32to64");
  }
}


void fftFromPhase() {
   static char *name={"  FFT from Phase    "};
   char *names[]={"Inp Phase FileName: ",
		  "Out Compl FileName: ",
		  "Direct {1,-1(inv)}: "};
   char *defaults[]={"ph.pic        ","compl.pic         ","1         "};
   if(!sedit(25,5,3,names,TmpPar,defaults,name))  return ;
   strcpy(InpFileName, TmpPar[0]);
   AdjustPicName(InpFileName);
   strcpy(OutFileName, TmpPar[1]);
   AdjustPicName(OutFileName);
   int direct;
   sscanf(TmpPar[2],"%d",&direct);
   sprintf(defaults[0],"%s",InpFileName);
   sprintf(defaults[1],"%s",OutFileName);
   sprintf(defaults[2],"%d",direct);

   int ret=fft2phdht(InpFileName,OutFileName,direct,0,0);
//  ( char *SrcPic,  char *ComplexPic, int DirInv, double Scale = 0);
//   sprintf(TmpStr," returned code %d",ret);
//   strbox(TmpStr);
   fftErrorMessage(ret);

//   Picture a(OutFileName); a.DelField("MinMax"); a.Close();

   UndoMenu(1) ;
}
void fftFromPhaseAmplitude() {
   char AmplFileName[30];
   static char *name={"  FFT from Phase&Amplitude  "};
   char *names[]={"Inp Phase FileName: ",
		  "Inp Ampl  FileName: ",
		  "Out Compl FileName: ",
		  "Direct {1,-1(inv)}: "};
   char *defaults[]={"ph.pic        ",
		     "ampl.pic      ",
		     "compl.pic     ","1       "};
   if(!sedit(25,5,4,names,TmpPar,defaults,name))  return ;
   strcpy(InpFileName, TmpPar[0]);
   AdjustPicName(InpFileName);
   strcpy(AmplFileName, TmpPar[1]);
   AdjustPicName(AmplFileName);
   strcpy(OutFileName, TmpPar[2]);
   AdjustPicName(OutFileName);
   int direct;
   sscanf(TmpPar[3],"%d",&direct);
   sprintf(defaults[0],"%s",InpFileName);
   sprintf(defaults[1],"%s",AmplFileName);
   sprintf(defaults[2],"%s",OutFileName);
   sprintf(defaults[3],"%d",direct);

       int ret = fft2mpdht(AmplFileName,InpFileName,OutFileName,direct,0,0 );

//  int ret =1;
//  ( char *SrcModPic,  char *SrcPhPic,  char *ComplexResPic,
//    int DirInv, double Scale = 0);
//   sprintf(TmpStr," returned code %d",ret);
//   strbox(TmpStr);
        fftErrorMessage(ret);

//   Picture a(OutFileName); a.DelField("MinMax"); a.Close();

   UndoMenu(1) ;
}
void fftFromComplex() {
   static char *name={"  FFT from Complex   "};
   char *names[]={"Inp Compl FileName: ",
		  "Out Compl FileName: ",
		  "Direct {1,-1(inv)}: "};
   char *defaults[]={"compl.pic       ",
		     "compl2.pic      ","1        "};
   if(!sedit(25,5,3,names,TmpPar,defaults,name))  return ;
   strcpy(InpFileName, TmpPar[0]);
   AdjustPicName(InpFileName);
   strcpy(OutFileName, TmpPar[1]);
   AdjustPicName(OutFileName);
   int direct;
   sscanf(TmpPar[2],"%d",&direct);
   sprintf(defaults[0],"%s",InpFileName);
   sprintf(defaults[1],"%s",OutFileName);
   sprintf(defaults[2],"%d",direct);

   int ret=fft2codht(InpFileName,OutFileName,direct,0,0);
//  ( char *SrcComplexPic, char *DstComplexPic, int DirInv, double Scale = 0 );
//   sprintf(TmpStr," returned code %d",ret);
//   strbox(TmpStr);
   fftErrorMessage(ret);

//   Picture a(OutFileName); a.DelField("MinMax"); a.Close();

   UndoMenu(1) ;
}

void fftFromReal() {
   static char *name={"FFT from Amplitude (phase=0)"};
   char *names[]={"Inp Real  FileName: ",
		  "Out Compl FileName: ",
		  "Direct {1,-1(inv)}: "};
   char *defaults[]={"real.pic       ",
		     "compl2.pic      ",
		     "1        "};
   if(!sedit(25,5,3,names,TmpPar,defaults,name))  return ;
   strcpy(InpFileName, TmpPar[0]);
   AdjustPicName(InpFileName);
   strcpy(OutFileName, TmpPar[1]);
   AdjustPicName(OutFileName);
   int direct;
   sscanf(TmpPar[2],"%d",&direct);
   sprintf(defaults[0],"%s",InpFileName);
   sprintf(defaults[1],"%s",OutFileName);
   sprintf(defaults[2],"%d",direct);

   int ret=fft2redht(InpFileName,OutFileName,direct,0,0);
//  ( char *SrcComplexPic, char *DstComplexPic, int DirInv, double Scale = 0 );
//   sprintf(TmpStr," returned code %d",ret);
//   strbox(TmpStr);
   fftErrorMessage(ret);

//   Picture a(OutFileName); a.DelField("MinMax"); a.Close();

   UndoMenu(1) ;
}

void ComplexToPhase() {
   static char *name={"Convert ComplexFile to PhaseFile"};
   char *names[]={"Inp Compl FileName:",
		  "Out Phase FileName:"};
   char *defaults[]={"compl.pic        ","ph.pic        "};
   if(!sedit(25,5,2,names,TmpPar,defaults,name))  return ;
   strcpy(InpFileName, TmpPar[0]);
   AdjustPicName(InpFileName);
   strcpy(OutFileName, TmpPar[1]);
   AdjustPicName(OutFileName);
   sprintf(defaults[0],"%s",InpFileName);
   sprintf(defaults[1],"%s",OutFileName);

   int ret=_64to_ph( InpFileName, OutFileName );
   fftErrorMessage(ret);
   UndoMenu(1) ;
}
void ComplexToAmplitude() {
   static char *name={"Convert ComplexFile to AmplitudeFile"};
   char *names[]={"Inp Compl FileName:",
		  "Out Ampl  FileName:"};
   char *defaults[]={"compl.pic        ","am.pic        "};
   if(!sedit(25,5,2,names,TmpPar,defaults,name))  return ;
   strcpy(InpFileName, TmpPar[0]);
   AdjustPicName(InpFileName);
   strcpy(OutFileName, TmpPar[1]);
   AdjustPicName(OutFileName);
   sprintf(defaults[0],"%s",InpFileName);
   sprintf(defaults[1],"%s",OutFileName);

   int ret=_64to_mod( InpFileName, OutFileName );
   fftErrorMessage(ret);
   UndoMenu(1) ;
}
